Imports System 
Imports System.ServiceModel

Namespace InSiteWebApplication 
    Public Class WCFOPC
        Private Shared Function ReadSingleTag(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String, ByVal tagName As String) As WCF2OPCService.ItemVTQ
            Try
                Dim address As New EndpointAddress(WCFTransportURI)
                Using factory As New ChannelFactory(Of WCF2OPCService.IWCFOPC)("legacySOAPWS", address)
                    ' Create a channel that is configured with this address and binding. 
                    Dim channel As WCF2OPCService.IWCFOPC = factory.CreateChannel()

                    Dim items As WCF2OPCService.ItemVTQ() = {New WCF2OPCService.ItemVTQ()}
                    items(0) = New WCF2OPCService.ItemVTQ()
                    items(0).ItemName = (fullyQulifiedName & ":") + tagName

                    channel.Read(items)
                    If items IsNot Nothing AndAlso items.Length > 0 Then
                        Return items(0)
                    End If
                End Using
            Catch
            End Try

            Return Nothing
        End Function

        Private Shared Sub WriteSingleTag(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String, ByVal tagName As String, ByVal tagValue As Object)
            Dim address As New EndpointAddress(WCFTransportURI)
            Using factory As New ChannelFactory(Of WCF2OPCService.IWCFOPC)("legacySOAPWS", address)
                ' Create a channel that is configured with this address and binding. 
                Dim channel As WCF2OPCService.IWCFOPC = factory.CreateChannel()

                Dim items As WCF2OPCService.ItemVTQ() = {New WCF2OPCService.ItemVTQ()}
                items(0) = New WCF2OPCService.ItemVTQ()
                items(0).ItemName = (fullyQulifiedName & ":") + tagName
                items(0).Value = tagValue

                Try
                    channel.Write(items)
                Catch
                End Try
            End Using
        End Sub
        Public Shared Function WCFOPCGetScaleStatus(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String) As Integer
            Dim returnValue As WCF2OPCService.ItemVTQ = ReadSingleTag(WCFTransportURI, fullyQulifiedName, "State")
            If returnValue IsNot Nothing AndAlso returnValue.Value IsNot Nothing Then
                Return Convert.ToInt32(returnValue.Value)
            Else
                Return 0
            End If
        End Function

        Public Shared Function WCFOPCTare(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String) As Integer
            WriteSingleTag(WCFTransportURI, fullyQulifiedName, "Tare", "1")
            Return WCFOPCGetScaleStatus(WCFTransportURI, fullyQulifiedName)
        End Function

        Public Shared Function WCFOPCBeginWeigh(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String) As Integer
            WriteSingleTag(WCFTransportURI, fullyQulifiedName, "WeighFlag", "1")
            Return WCFOPCGetScaleStatus(WCFTransportURI, fullyQulifiedName)
        End Function

        Public Shared Function WCFOPCGetWeight(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String, ByRef isQualityGood As Boolean) As Double
            isQualityGood = False
            Dim returnValue As WCF2OPCService.ItemVTQ = ReadSingleTag(WCFTransportURI, fullyQulifiedName, "Weight")
            If returnValue IsNot Nothing Then
                isQualityGood = returnValue.QualityGood
                If returnValue.Value IsNot Nothing Then
                    Return Convert.ToDouble(returnValue.Value)
                End If
            End If

            Return 0.0R
        End Function

        Public Shared Sub WCFOPCFinishWeigh(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String)
            WriteSingleTag(WCFTransportURI, fullyQulifiedName, "WeighFlag", "0")
        End Sub

        Public Shared Sub WCFOPCSetPollRate(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String, ByVal pollRate As Double)
            WriteSingleTag(WCFTransportURI, fullyQulifiedName, "PollRate", pollRate)
        End Sub

        Public Shared Function WCFOPCGetCaptureWeight(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String, ByRef isQualityGood As Boolean) As Double
            isQualityGood = False
            Dim returnValue As WCF2OPCService.ItemVTQ = ReadSingleTag(WCFTransportURI, fullyQulifiedName, "CaptureWeight")
            If returnValue IsNot Nothing Then
                isQualityGood = returnValue.QualityGood
                If returnValue.Value IsNot Nothing Then
                    Return Convert.ToDouble(returnValue.Value)
                End If
            End If

            Return 0.0R
        End Function

        Public Shared Function WCFOPCPrepareCaptureWeight(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String) As Integer
            WriteSingleTag(WCFTransportURI, fullyQulifiedName, "PrepareCaptureWeight", "1")
            Return WCFOPCGetScaleStatus(WCFTransportURI, fullyQulifiedName)
        End Function

        Public Shared Function WCFOPCGetCaptureWeightValueChangedFlag(ByVal WCFTransportURI As String, ByVal fullyQulifiedName As String) As Integer
            Dim returnValue As WCF2OPCService.ItemVTQ = ReadSingleTag(WCFTransportURI, fullyQulifiedName, "CaptureWeightValueChanged")
            If returnValue IsNot Nothing AndAlso returnValue.Value IsNot Nothing Then
                Return Convert.ToInt32(returnValue.Value)
            Else
                Return 0
            End If
        End Function
    End Class
End Namespace 
